package com.hwadee.ems.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hwadee.ems.domain.dto.PageQuery;
import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.po.ScheduledCourse;
import com.hwadee.ems.domain.vo.ScheduledCourseVO;
import com.hwadee.ems.service.ICourseService;
import com.hwadee.ems.service.IScheduledCourseService;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 已排课课程 前端控制器
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Slf4j
@RestController
@RequestMapping("/scheduled_course")
public class ScheduledCourseController {

    @Resource
    private IScheduledCourseService scheduledCourseService;

    @Resource
    private ICourseService courseService;

    @GetMapping("/page")
    public Result<IPage<ScheduledCourseVO>> page(
        PageQuery pageQuery,
        @RequestParam(required = false) String courseName,
        @RequestParam(required = false) Long semesterId,
        @RequestParam(required = false) String classroomName
    ) {
        return scheduledCourseService.page(pageQuery, courseName, semesterId, classroomName);
    }

    @PostMapping
    public Result<Long> save(@RequestBody ScheduledCourse scheduledCourse) {
        if (courseService.getById(scheduledCourse.getCourseId()) == null) {
            return Result.error("课程不存在");
        }
        if (scheduledCourseService.save(scheduledCourse)) {
            log.info("保存排课成功，id={}", scheduledCourse.getId());
            return Result.ok(scheduledCourse.getId());
        } else {
            log.error("保存排课失败，id={}", scheduledCourse.getId());
            return Result.error();
        }
    }

    @PutMapping
    public Result<Void> updateById(@RequestBody ScheduledCourse scheduledCourse) {
        if (courseService.getById(scheduledCourse.getCourseId()) == null) {
            return Result.error("课程不存在");
        }
        if (scheduledCourseService.updateById(scheduledCourse)) {
            log.info("更新排课成功，id={}", scheduledCourse.getId());
            return Result.ok();
        } else {
            log.error("更新排课失败，id={}", scheduledCourse.getId());
            return Result.error();
        }
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> removeById(@PathVariable Long id) {
        if (scheduledCourseService.removeById(id)) {
            log.info("删除排课成功，id={}", id);
            return Result.ok();
        } else {
            log.error("删除排课失败，id={}", id);
            return Result.error();
        }
    }

    @GetMapping("/page_by_teacher_id")
    public Result<IPage<ScheduledCourseVO>> pageByTeacherId(
        PageQuery pageQuery,
        @RequestParam Long teacherId, 
        @RequestParam(required = false) String courseName,
        @RequestParam(required = false) Long semesterId,
        @RequestParam(required = false) String classroomName
    ) {
        return scheduledCourseService.pageByTeacherId(
            pageQuery, 
            teacherId,
            courseName,
            semesterId,
            classroomName
        );
    }

    @GetMapping("/page_by_student_id")
    public Result<IPage<ScheduledCourseVO>> pageByStudentId(
        PageQuery pageQuery,
        @RequestParam Long studentId, 
        @RequestParam(required = false) String courseName,
        @RequestParam(required = false) Long semesterId,
        @RequestParam(required = false) String classroomName
    ) {
        return scheduledCourseService.pageByStudentId(
            pageQuery, 
            studentId,
            courseName,
            semesterId,
            classroomName
        );
    }


    @GetMapping("/list")
    public Result<List<ScheduledCourseVO>> list() {
        return Result.ok(
            scheduledCourseService.list()
                .stream()
                .map(ScheduledCourseVO::new)
                .collect(Collectors.toList())
        );
    }

}